//
//  GMChoseFileVC.m
//  Teecloud
//
//  Created by xhkj on 2018/11/24.
//  Copyright © 2018 小志. All rights reserved.
//

#import "GMChoseFileVC.h"
#import "GMChoseFileCell.h"
@interface GMChoseFileVC ()<UITableViewDataSource,UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UITableView *tabView;
@property (nonatomic,strong) NSMutableArray *dataArr;
@end

@implementation GMChoseFileVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.tabView registerNib:[UINib nibWithNibName:@"GMChoseFileCell" bundle:nil] forCellReuseIdentifier:@"GMChoseFileCell"];
    //文件操作对象
    NSFileManager *manager = [NSFileManager defaultManager];
    //拼接缓存目录
    NSString *downloadDir = [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject]stringByAppendingPathComponent:TENYUNDOWN];
    //目录迭代器
    NSDirectoryEnumerator *direnum = [manager enumeratorAtPath:downloadDir];
    //快速枚举
    for (NSString *filename in direnum) {
        NSString *pathStr = [NSString stringWithFormat:@"%@/%@",downloadDir,filename];
        NSInteger size =  [self fileSizeAtPath:pathStr];
        NSString *sizeStr = [self transformedValue:size];
        NSString *imgStr = @"";
        if ([filename containsString:@"zip"]) {
            imgStr =@"doc-zip";
        }else if ([filename containsString:@"pdf"]) {
             imgStr =@"doc-pdf";
        }else if ([filename containsString:@"ppt"]) {
             imgStr =@"doc-ppt";
        }else if ([filename containsString:@"docx"]||[filename containsString:@"doc"]) {
            imgStr =@"doc-word";
        }else if ([filename containsString:@"excel"]) {
            imgStr =@"doc-excel";
        }else if ([filename containsString:@"rar"]) {
            imgStr =@"doc-rar";
        }else{
            imgStr =@"doc-other";
        }
        NSDictionary *dict = @{
                               @"name":filename,
                               @"size":sizeStr,
                               @"path":pathStr,
                               @"image":imgStr
                               };
        [self.dataArr addObject:dict];
    }
    if (self.dataArr.count == 0) {
        [self.tabView setupEmptyDataText:@"暂无数据" secoundText:@"" buttonTit:@"" verticalOffset:0 emptyImage:NODATAIMG tapBlock:^{
            
        } butClick:^{
            
        }];
    }
    [self.tabView reloadData];
}
/**获取指定路径文件大小*/
-(long long) fileSizeAtPath:(NSString*)filePath{
    NSFileManager* manager =[NSFileManager defaultManager];
    if ([manager fileExistsAtPath:filePath]){
        return [[manager attributesOfItemAtPath:filePath error:nil]fileSize];
    }
    return 0;
}
/**把文件字节转换成MB*/
- (NSString *)transformedValue:(NSInteger )value{
    double convertedValue =value;
    int multiplyFactor = 0;
    NSArray *tokens = [NSArray arrayWithObjects:@"bytes",@"KB",@"MB",@"GB",@"TB",@"PB", @"EB" @"ZB", @"YB",nil];
    while (convertedValue > 1024) {
        convertedValue /= 1024;
        multiplyFactor++;
    }
    
    return [NSString stringWithFormat:@"%4.2f %@",convertedValue, [tokens objectAtIndex:multiplyFactor]];

}
-(NSMutableArray *)dataArr{
    if (!_dataArr) {
        _dataArr = [[NSMutableArray alloc] init];
    }
    return _dataArr;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArr.count;
    
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    GMChoseFileCell *cell = [tableView dequeueReusableCellWithIdentifier:@"GMChoseFileCell" forIndexPath:indexPath];
    NSDictionary *temDict = self.dataArr[indexPath.row];
    NSString *temStr = [temDict objectForKey:@"name"];
    cell.name.text = temStr;
    cell.sizeStr.text = [temDict objectForKey:@"size"];
    cell.img.image =[UIImage imageNamed:[temDict objectForKey:@"image"]];
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 80;
}
-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSDictionary *temDict = self.dataArr[indexPath.row];
    self.GMChoseFileVCBlock(temDict);
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}
- (IBAction)backBtnClick:(id)sender {
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}


@end
